unit MVC.Timing;

interface

uses
  System.SysUtils, System.Classes, System.Generics.Collections, IdCoderMIME,
  MVC.LogUnit, MVC.JSON, MVC.Config, System.JSON, IOUtils;

type
  TAction = procedure of object;

  TTiming = class(TThread)
  private
    actionlist: TDictionary<string, TAction>;
    procedure delReport;
  protected
    procedure Execute; override;
  public
    constructor Create();
    destructor Destroy; override;

  end;

var
  FTiming: TTiming;

implementation

{ TTiming }

constructor TTiming.Create();
begin
  inherited Create(false);
  actionlist := TDictionary<string, TAction>.Create;
  actionlist.Add('17:27:00', delReport);
end;

procedure TTiming.delReport;
var
  path: string;
begin
  path := '.\ReportTmp';
  if TDirectory.Exists(path) then
    TDirectory.Delete(path,True);
end;

destructor TTiming.Destroy;
begin
  actionlist.Free;
  inherited;
end;

procedure TTiming.Execute;
var
  key: string;
  action: TAction;
  newtime, oldtime: string;
begin
  inherited;
  oldtime := FormatDateTime('HH:mm:ss', Now);
  while not Terminated do
  begin

    try
      newtime := FormatDateTime('HH:mm:ss', Now);
      if (newtime <> oldtime) then
      begin
//        Log(newtime);
        for key in actionlist.Keys do
        begin
          if newtime = key then
          begin
            action := actionlist[key];
            action;
          end;
        end;
      end;
    finally
      oldtime := newtime;
      Sleep(10);
    end;
  end;
end;

initialization
  FTiming := TTiming.Create;

finalization
  FTiming.Free;

end.

